package cn.bonoon.kernel.web.handlers;

import cn.bonoon.handler.impl.StandardAutoManager;
import cn.bonoon.kernel.events.OperateEvent;
import cn.bonoon.kernel.support.services.OperateService;
import cn.bonoon.kernel.web.ButtonEventType;
import cn.bonoon.kernel.web.controllers.GridButtonResolver;
import cn.bonoon.kernel.web.controllers.StatusButtonResolver;
import cn.bonoon.kernel.web.models.IdParameter;

/**
 * <pre>
 * 处理单个数据库对象(或数据库表)的操作事件处理器；
 * 如：删除某个实体、移动某个实体等操作
 * </pre>
 * @author jackson
 *
 * @param <P>
 */
public class OperateIdHandler extends AbstractOperateAjaxHandler{

	private OperateIdHandler(int operand, OperateService operateService, StandardAutoManager initializer) {
		super(operand, operateService, initializer);
	}
	
	public OperateIdHandler(int operand, String name, OperateService operateService, StandardAutoManager initializer) {
		super(operand, name, operateService, initializer);
	}

	@Override
	public IdParameter buildParams() {
		return new IdParameter();
	}
	
	@Override
	protected Object internalExecute(OperateEvent event) {
		return operateService.operate(operandCode, event, IdParameter.toId(event));
	}
	
	@Override
	public StatusButtonResolver register(StatusButtonResolver resolver) {
		resolver.setEventType(ButtonEventType.GET);
		return super.register(resolver);
	}
	
	public static GridButtonResolver button(HandlerRegister register, OperateIdHandler handler){
		GridButtonResolver gbr = register.button();
		handler.register(gbr);
		return gbr;
	}
	
	public static GridButtonResolver delete(HandlerRegister register, OperateService operateService)throws Exception{
		OperateIdHandler deleteHandler = new OperateIdHandler(OPERAND_DELETE, operateService, register.getManager());
		return button(register, deleteHandler);
	}
	
	public static GridButtonResolver forbid(HandlerRegister register, OperateService operateService)throws Exception{
		OperateIdHandler forbidHandler = new OperateIdHandler(OPERAND_FORBID, operateService, register.getManager());
		GridButtonResolver gbr = button(register, forbidHandler);
		gbr.status(1);
		return gbr;
	}
	
	public static GridButtonResolver resume(HandlerRegister register, OperateService operateService)throws Exception{
		OperateIdHandler resumeHandler = new OperateIdHandler(OPERAND_RESUME, operateService, register.getManager());
		GridButtonResolver gbr = button(register, resumeHandler);
		gbr.status(-1, 0);
		return gbr;
	}
}
